package com.pinfly.common.util;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

/**
 * DateUtil This class provides some command static methods that will be used
 * for date manipulation.
 * 
 * @version $Revision: 1082 $
 */
public class DateUtil
{
    /**
     * formatDate Formats a date in the short format.
     * 
     * @param d the date to format
     * @param locale the locale to use
     * @return the date formatted for the locale
     */
    public static String formatShortDate (Date d, Locale locale)
    {
        if (d == null)
        {
            return "";
        }

        DateFormat df = DateFormat.getDateTimeInstance (DateFormat.SHORT, DateFormat.MEDIUM, locale);
        return df.format (d);
    }

    /**
     * formatMedDate Formats a date in the med format.
     * 
     * @param d the date to format
     * @param locale the locale to format for
     * @return the date formatted for the locale
     */
    public static String formatMedDate (Date d, Locale locale)
    {
        if (d == null)
        {
            return "";
        }

        DateFormat df = DateFormat.getDateTimeInstance (DateFormat.MEDIUM, DateFormat.MEDIUM, locale);
        return df.format (d);
    }

    /**
     * getCurrDate Returns the current Date
     * 
     * @return the current date as a Date
     */
    public static Date getCurrDate ()
    {
        return new Date ();
    }

    /**
     * getCurrTime Returns the current time in format specified.
     * 
     * @return the current time in specified format
     */
    public static String getCurrTime (Date d, String timeFormat)
    {
        SimpleDateFormat dformat = new SimpleDateFormat (timeFormat);
        return dformat.format (d);
    }

    /**
     * Returns the diffence in milliseconds or a -1.
     * 
     * @param st the start time
     * @param end the end time
     * @return the amount of time between start and end
     */
    public static long getDiffms (long st, long end)
    {
        if (st > end)
        {
            return (-1);
        }
        return (end - st);
    }

    /**
     * getDiffms Returns the diffence in milliseconds or a -1.
     * 
     * @param st the start date
     * @param end the end date
     * @return the amount of time between start and end
     */
    public static long getDiffms (Date st, Date end)
    {
        return getDiffms (st.getTime (), end.getTime ());
    }

}
